package com.wry.myphotowall.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.wry.myphotowall.R;
import com.wry.myphotowall.listener.OnItemClickListerner;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13 0013.
 */

public class PhotoWallAdapter extends RecyclerView.Adapter<PhotoWallAdapter.ViewHolder> {

    /**
     * 传入的本机相册图片
     */
    public static List<String> mAllImage = new LinkedList<>();
    /**
     * 用户选择的图片,存储为图片的完整路径
     */
    public static List<String> mSelectedImage = new ArrayList<>();
    /**
     * 文件夹路径
     */
    private String mDirPath;
    /**
     * 选择图片数量限制
     */
    private int mSelectCount;
    /**
     * 回调接口方法
     */
    private OnItemClickListerner mOnItemClickListerner = null;

    private boolean isShowCamera = true;
    public final static int ITEM_TYPE_CAMERA = 100;
    public final static int ITEM_TYPE_PHOTO = 101;

    private Context mContext;

    public PhotoWallAdapter(Context context, List<String> mAllImage, String dirPath, int selectCount, boolean isShowCamera) {
        this.mContext = context;
        this.mAllImage = mAllImage;
        this.mDirPath = dirPath;
        this.mSelectCount = selectCount;
        this.isShowCamera = isShowCamera;
    }

    @Override
    public int getItemViewType(int position) {
        return (isShowCamera && position == 0) ? ITEM_TYPE_CAMERA : ITEM_TYPE_PHOTO;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item_image, parent, false);
        ViewHolder vh = new ViewHolder(view);
        if (viewType == ITEM_TYPE_CAMERA) {
            vh.mMark.setVisibility(View.GONE);
            vh.mCheckMark.setVisibility(View.GONE);
            vh.mImage.setScaleType(ImageView.ScaleType.CENTER);

            vh.mImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListerner != null) {
                        mOnItemClickListerner.onCameraClick(view);    //回调
                    }
                }
            });
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (getItemViewType(position) == ITEM_TYPE_PHOTO) {
            holder.mMark.setVisibility(View.GONE);
            String item = "";
            if (isShowCamera) {
                item = mAllImage.get(position - 1);
            } else {
                item = mAllImage.get(position);
            }
            ImageRequest request = ImageRequestBuilder
                    .newBuilderWithSource(Uri.parse("file://" + mDirPath + "/" + item))
                    .setResizeOptions(new ResizeOptions(200, 200))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder().setOldController(holder.mImage.getController()).setImageRequest(request)
                    .build();
            holder.mImage.setController(controller);
            //设置Mark的点击事件
            final String finalItem = item;
            holder.mCheckMark.setOnClickListener(new View.OnClickListener() {
                //选择，则将图片变暗，反之则反之
                @Override
                public void onClick(View v) {
                    //已经选择过该图片
                    if (mSelectedImage.contains(mDirPath + "/" + finalItem)) {
                        mSelectedImage.remove(mDirPath + "/" + finalItem);
                        mOnItemClickListerner.onMarkClick(mDirPath + "/" + finalItem);    //回调
                        holder.mCheckMark.setImageResource(R.drawable.btn_unselected);
                        holder.mMark.setVisibility(View.GONE);
                    } else {
                        //未选择该图片
                        if (mSelectedImage.size() == mSelectCount) {
                            Toast.makeText(mContext, R.string.msg_amount_limit,
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        mSelectedImage.add(mDirPath + "/" + finalItem);
                        mOnItemClickListerner.onMarkClick(mDirPath + "/" + finalItem);    //回调
                        holder.mCheckMark.setImageResource(R.drawable.btn_selected);
                        holder.mMark.setVisibility(View.VISIBLE);
                    }
                }
            });

            //已经选择过的图片，显示出选择效果
            if (mSelectedImage.contains(mDirPath + "/" + item)) {
                holder.mCheckMark.setImageResource(R.drawable.btn_selected);
                holder.mMark.setVisibility(View.VISIBLE);
            } else {
                holder.mCheckMark.setImageResource(R.drawable.btn_unselected);
                holder.mMark.setVisibility(View.GONE);
            }

            //设置photo的点击事件
            holder.mImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListerner.onPhotoClick(v, position);
                }
            });
        } else {
            holder.mImage.setImageResource(R.drawable.__picker_camera);
        }
    }


    @Override
    public int getItemCount() {
        int photosCount = mAllImage.size() == 0 ? 0 : mAllImage.size();
        if (isShowCamera) {
            return photosCount + 1;
        }
        return photosCount;
    }


    /**
     * 回调方法
     *
     * @param onItemClickListerner
     */
    public void setOnItemClickListerner(OnItemClickListerner onItemClickListerner) {
        this.mOnItemClickListerner = onItemClickListerner;
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mCheckMark;
        public View mMark;
        public SimpleDraweeView mImage;

        public ViewHolder(View view) {
            super(view);
            mCheckMark = (ImageView) view.findViewById(R.id.iv_checkmark);
            mImage = (SimpleDraweeView) view.findViewById(R.id.siv_image);
            mMark = (View) view.findViewById(R.id.mask);
        }
    }
}
